package com.jianshuai.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.jianshuai.domain.User;
import com.jianshuai.fallback.DowngradeUtil;
import com.jianshuai.service.IUserService;
import com.jianshuai.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Value("${server.port}")
    private String port;


    @Autowired
    private IUserService iUserService;
    @PostMapping("/add")
    public AjaxResult add(@RequestBody User user){
        return iUserService.add(user);
    }

    @DeleteMapping("/delete/{userId}")
    public AjaxResult delete(@PathVariable("userId") Long userId){
        return iUserService.delete(userId);
    }

    @GetMapping("/getUserById/{userId}")
    public AjaxResult  getUserById(@PathVariable("userId") Long userId){
        AjaxResult ajaxResult = iUserService.getUserById(userId);
        User data = (User) ajaxResult.getData();
        data.setProt(port);
        ajaxResult.setData(data);
        return ajaxResult;
    }

    @SentinelResource(value = "user-limit",blockHandler = "fallbackMethod1",blockHandlerClass = DowngradeUtil.class)
    @GetMapping("/getId/{userId}")
    public User getId(@PathVariable("userId") Long userId){
        User user = iUserService.getId(userId);
        user.setProt(port);
        return user;
    }

    @PostMapping("/update")
    public AjaxResult update(@RequestBody User user){
        return iUserService.update(user);
    }

}
